package com.ywm.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywm.eduService.entity.EduChapter;
import com.ywm.eduService.entity.EduVideo;
import com.ywm.eduService.entity.vo.ChapterVo;
import com.ywm.eduService.mapper.EduChapterMapper;
import com.ywm.eduService.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywm.eduService.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-18
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired//注入小节
    private EduVideoService videoService;
    @Override
    // 查询所有章节和小节
    public List<ChapterVo> getChapterVideoByCourseId(String id) {
        // 1.所有的章节
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",id);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapper);
        // 2. 遍历章节进行封装
        List<ChapterVo> finalList=new ArrayList<>();
        for (EduChapter eduChapter : eduChapterList) {
            List<ChapterVo> list2=new ArrayList<>();
            ChapterVo chapterVo = new ChapterVo(eduChapter.getId(), eduChapter.getTitle(),null, null);
            QueryWrapper<EduVideo> wrapper2=new QueryWrapper<>();
            wrapper2.eq("chapter_id",eduChapter.getId());
            List<EduVideo> eduVideoList = videoService.list(wrapper2);
            // 遍历每个章节的小节
            for (EduVideo eduVideo : eduVideoList) {
                list2.add(new ChapterVo(eduVideo.getId(),eduVideo.getTitle(),eduVideo.getVideoSourceId(),null));
            }
            chapterVo.setChildren(list2);
            finalList.add(chapterVo);
        }
        return finalList;
    }
    //删除章节
    @Override
    public void deleteChapter(String id) {

    }
    //1.根据课程ID删除章节
    @Override
    public void removeChapterVyCoursrId(String id) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        baseMapper.delete(wrapper);
    }
}
